package com.solo.eggsolo.service.impl;

import java.util.HashMap;
import java.util.List;

import org.attoparser.config.ParseConfiguration.UniqueRootElementPresence;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.solo.eggsolo.entity.Singer;
import com.solo.eggsolo.entity.User;
import com.solo.eggsolo.mapper.UserMapper;
import com.solo.eggsolo.service.UserService;
import com.solo.eggsolo.util.JwtUtil;

import cn.hutool.json.JSONException;
/**
 * 
 * @author Yu
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService{
	
	@Autowired
	private UserMapper userMapper;
	
	
	@Override
	public String login(String username,String password) {
		
		User user = userMapper.selectUserByUsername(username);
		
		if(user == null) {
			throw new JSONException("用户名不存在");
		}
		
		if(!user.getPassword().equals(password)) {
			throw new JSONException("密码错误");
		}
		
		HashMap<String, Object> userInfo = new HashMap<>();
		userInfo.put("role", "用户");
		userInfo.put("id", user.getId());
		userInfo.put("name", user.getName());
		
		return JwtUtil.sign(username,userInfo);
		
	}
	
	@Override
	public User userInfo(String token) {
		String username = JwtUtil.getUserId(token);
		
		return userMapper.selectUserByUsername(username);
	}
	
	@Override
	public List<User> findByName(String name) {
		return userMapper.findByName(name);
	}
	
	@Override
	public boolean resetPassword(Integer id) {
		return userMapper.resetPassword(id);
	}

}
